package com.luocj.brvah_srl.activity;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.luocj.brvah_srl.R;
import com.luocj.brvah_srl.fragment.FragmentA;
import com.luocj.brvah_srl.fragment.FragmentB;
import com.luocj.brvah_srl.fragment.FragmentBoss;

import java.util.ArrayList;

public class BossActivity extends AppCompatActivity {

    private Button btn1;
    private Button btn2;
    private ViewPager viewpager;
    private ArrayList<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_boss);
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        viewpager = findViewById(R.id.viewpager);
        fragments = new ArrayList<>();
//        fragments.add(new FragmentA());
        fragments.add(new FragmentB());
        fragments.add(new FragmentBoss());

        viewpager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    btn1.setBackgroundColor(Color.RED);
                    btn2.setBackgroundColor(Color.WHITE);
                } else if (position == 1) {
                    btn2.setBackgroundColor(Color.RED);
                    btn1.setBackgroundColor(Color.WHITE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewpager.setCurrentItem(0);
                btn1.setBackgroundColor(Color.RED);
                btn2.setBackgroundColor(Color.WHITE);
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewpager.setCurrentItem(1);
                btn2.setBackgroundColor(Color.RED);
                btn1.setBackgroundColor(Color.WHITE);
            }
        });

    }

    private class MyPagerAdapter extends FragmentStatePagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }
}
